package com.example.zz.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zz.entity.Notic;
import com.example.zz.entity.Student;
import com.example.zz.entity.Teacher;
import com.example.zz.mapper.*;
import com.example.zz.service.IwService;
import com.example.zz.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: cms
 * @Author: wuyuhang
 * @create: 2021-02-02 20:43
 */
@Service
public class WServiceImpl implements IwService {

    @Autowired
    NoticMapper noticMapper;
    @Autowired
    SchoolMapper schoolMapper;

    @Autowired
    StudentMapper studentMapper;
    @Autowired
    TeacherMapper teacherMapper;

    @Autowired
    ClassesMapper classesMapper;
    @Autowired
    CollegeMapper collegeMapper;


    /**
     * 查询通知列表
     * @param wrapper
     * @param current
     * @param limit
     * @return
     */
    public PageVO<Notic> noticGetList(QueryWrapper<Notic> wrapper, Integer current, Integer limit){
        if(current == null || current <= 0){
            current = 1;
        }
        if(limit == null || limit < 0 || limit > 100){
            limit = 20;
        }
        Page<Notic> page = new Page<>(current, limit);
        noticMapper.selectPage(page, wrapper);
        List<Notic> noticList = new ArrayList<>();
        for(Notic n: page.getRecords()){
            Notic temp = new Notic();
            temp.setId(n.getId());
            temp.setPraise(n.getPraise());
            temp.setState(n.getState());
            temp.setText(n.getText());
            temp.setCreateTime(n.getCreateTime());
            temp.setSchool(schoolMapper.selectById(n.getSchoolId()));
            noticList.add(temp);
        }
        PageVO<Notic> vo = new PageVO<>();
        vo.setCode(0);
        vo.setMsg("");
        vo.setCount(page.getTotal());
        vo.setData(noticList);
        return vo;
    }
    /**
     * 根据id获取通知
     * @param id
     * @return
     */
    public Notic noticGet(Integer id){
        Notic notic = noticMapper.selectById(id);
        if(notic == null){
            return null;
        }
        notic.setSchool(schoolMapper.selectById(notic.getSchoolId()));
        return notic;

    }
    /**
     * 新增通知
     * @param notic
     * @return
     */
    public Boolean noticAdd(Notic notic){
        if(StringUtils.isEmpty(notic.getText())){
            return false;
        }
        if(notic.getPraise() == null){
            return false;
        }
        if(notic.getSchoolId() == null){
            return false;
        }
        if(notic.getState() == null){
            return false;
        }
        return noticMapper.insert(notic) == 1;
    }
    /**
     * 修改通知
     * @param notic
     * @param id
     * @return
     */
    public Boolean noticEdit(Notic notic, Integer id){

        Notic old = noticMapper.selectById(id);
        if(old == null){
            return false;
        }
        if(!old.getSchoolId().equals(notic.getSchoolId())){
            return false;
        }

        old.setState(notic.getState());
        old.setText(notic.getText());
        old.setPraise(notic.getPraise());
        return noticMapper.updateById(notic) == 1;

    }
    /**
     * 删除通知
     * @param id
     * @return
     */
    public Boolean noticDelete(Integer id){
        return noticMapper.deleteById(id) == 1;
    }

    @Override
    public PageVO<Student> stuGetList(QueryWrapper<Student> wrapper, Integer current, Integer limit) {
        if(current == null || current <= 0){
            current = 1;
        }
        if(limit == null || limit < 0 || limit > 100){
            limit = 20;
        }
        Page<Student> page = new Page<>(current, limit);
        studentMapper.selectPage(page, wrapper);
        List<Student> studentList = new ArrayList<>();
        for(Student s: page.getRecords()){
            Student temp = new Student();
            temp.setClassId(s.getClassId());
            temp.setStuSex(s.getStuSex());
            temp.setClasses(classesMapper.selectById(s.getClassId()));
            temp.setStuName(s.getStuName());
            temp.setId(s.getId());
            temp.setStuId(s.getStuId());
            temp.setCreateTime(s.getCreateTime());
            temp.setUpdateTime(s.getUpdateTime());
            studentList.add(temp);
        }
        PageVO<Student> vo = new PageVO<>();
        vo.setCode(0);
        vo.setMsg("");
        vo.setCount(page.getTotal());
        vo.setData(studentList);
        return vo;
    }

    @Override
    public Student stuGet(Integer id) {
        return studentMapper.selectById(id);
    }

    @Override
    public Boolean stuAdd(Student stu) {
        int result = studentMapper.insert(stu);
        return result > 0;
    }

    @Override
    public Boolean stuEdit(Student stu, Integer id) {
        Student old = studentMapper.selectById(id);
        if(old == null){
            return false;
        }
        old.setStuName(stu.getStuName());
        old.setStuSex(stu.getStuSex());
        old.setStuId(stu.getStuId());
        old.setClassId(stu.getClassId());
        old.setClassId(old.getClassId());
        return studentMapper.updateById(old) == 1;
    }

    @Override
    public Boolean stuDelete(Integer id) {
        return studentMapper.deleteById(id) > 0;
    }

    @Override
    public PageVO<Teacher> teaGetList(QueryWrapper<Teacher> wrapper, Integer current, Integer limit) {
        if(current == null || current <= 0){
            current = 1;
        }
        if(limit == null || limit < 0 || limit > 100){
            limit = 20;
        }
        Page<Teacher> page = new Page<>(current, limit);
        teacherMapper.selectPage(page, wrapper);
        List<Teacher> teacherList = new ArrayList<>();
        for(Teacher s: page.getRecords()){
            Teacher temp = new Teacher();
            temp.setCollegeId(s.getCollegeId());
            temp.setCollege(collegeMapper.selectById(s.getCollegeId()));
            temp.setTeaName(s.getTeaName());
            temp.setTeaSex(s.getTeaSex());
            temp.setTeaId(s.getTeaId());
            temp.setId(s.getId());
            temp.setLevel(s.getLevel());
            temp.setCreateTime(s.getCreateTime());
            temp.setUpdateTime(s.getUpdateTime());
            teacherList.add(temp);
        }
        PageVO<Teacher> vo = new PageVO<>();
        vo.setCode(0);
        vo.setMsg("");
        vo.setCount(page.getTotal());
        vo.setData(teacherList);
        return vo;
    }

    @Override
    public Teacher teaGet(Integer id) {
        return teacherMapper.selectById(id);
    }

    @Override
    public Boolean teaAdd(Teacher tea) {
        return teacherMapper.insert(tea) > 0;
    }

    @Override
    public Boolean teaEdit(Teacher tea, Integer id) {
        Teacher old = teacherMapper.selectById(id);
        if(old == null){
            return false;
        }
        old.setLevel(tea.getLevel());
        old.setTeaName(tea.getTeaName());
        old.setCollegeId(tea.getCollegeId());
        old.setTeaId(tea.getTeaId());
        old.setTeaSex(tea.getTeaSex());
        return teacherMapper.updateById(old) == 1;
    }

    @Override
    public Boolean teaDelete(Integer id) {
        return teacherMapper.deleteById(id) > 0;
    }
}